package com.syscom.server.qc.cacheManager;

import java.util.Hashtable;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.syscom.qcRule.util.GetQCRuleUtil;
import com.syscom.qcRule.util.QCRuleCal;
import com.syscom.sar.server.rsk.vo.OdrInfo;
import com.syscom.sar.server.rsk.vo.SarCODT;
import com.syscom.sar.server.rsk.vo.SarNBST;
import com.syscom.shared.constant.LogConstant;

/**
 * @author Jean
 */
public class QCManager {
	@SuppressWarnings("unused")
	private transient static final String mark = "10BCC85DDC54DDF27A18E78A2AA7EB50A27C9789E7CDBE9FD4DD9C9DC56D7247A585625B8AE77885";
	private static Logger mLogger = Logger.getLogger(LogConstant.SAR_SERVER_LOGGER);
	private static QCManager instance = new QCManager();
	
	public static QCManager getInstance() { return instance; }
	
	private QCManager() {}
	
	// General
	public Hashtable<Integer, double[]> receveGeneral(OdrInfo odrInfo, Hashtable<Integer, double[]> tblNeedToAccu) {
		/*
		 * TODO
		 * 進來要先加減共用的累計值, 這段暫時不作
		 * 2603 - 2606 才會用到，暫時不作 (國票不用)
		 */
		
		Hashtable<Integer, double[]> tblQC = new Hashtable<Integer, double[]> ();
		
		if (tblNeedToAccu == null || tblNeedToAccu.size() == 0) return tblQC;
		
		Integer[] iKeys = new Integer[tblNeedToAccu.size()];
		tblNeedToAccu.keySet().toArray(iKeys);
		
		for (int i = 0; i < iKeys.length; i++) {
			int iRuleId = iKeys[i];
			double[] dValues = tblNeedToAccu.get(iRuleId);
			
			QCRuleCal ruleCal = GetQCRuleUtil.getInstance(iRuleId);
			double[] dAccuRuleValues = ruleCal.calGeneral(iRuleId, dValues, odrInfo);
			
			if (dAccuRuleValues != null) tblQC.put(iRuleId, dAccuRuleValues);
		}
		
		return tblQC;
	}

	// CODT
	public Hashtable<Integer, double[]> receveCODT(SarCODT codtVo, Hashtable<Integer, double[]> tblNeedToAccu) {
		/*
		 * TODO
		 * 進來要先加減共用的累計值, 這段暫時不作
		 * 2603 - 2606 才會用到，暫時不作 (國票不用)
		 */
		
		Hashtable<Integer, double[]> tblQC = new Hashtable<Integer, double[]> ();
		
		if (tblNeedToAccu == null || tblNeedToAccu.size() == 0) return tblQC;
		
		Integer[] iKeys = new Integer[tblNeedToAccu.size()];
		tblNeedToAccu.keySet().toArray(iKeys);
		
		for (int i = 0; i < iKeys.length; i++) {
			int iRuleId = iKeys[i];
			double[] dValues = tblNeedToAccu.get(iRuleId);
			
			QCRuleCal ruleCal = GetQCRuleUtil.getInstance(iRuleId);
			double[] dAccuRuleValues = ruleCal.calCODT(iRuleId, codtVo, dValues);
			
			if (dAccuRuleValues != null) tblQC.put(iRuleId, dAccuRuleValues);
		}
		
		return tblQC;
	}
	
	// NBST
	public Hashtable<Integer, double[]> receveNBST(SarNBST nbstVo, Hashtable<Integer, double[]> tblNeedToAccu) {
		try {
			AccuNBST4CacheManager.getInstance().updateAccuNBST(nbstVo);
		}
		catch (Exception e) {
			if (mLogger.isEnabledFor(Level.ERROR)) {
				mLogger.log(Level.ERROR, "<" + nbstVo.getTradeNumber() + "> Update failed !! failed msg :" + e.getMessage(), e);
        	}
		}
		
		Hashtable<Integer, double[]> tblQC = new Hashtable<Integer, double[]> ();
		
		if (tblNeedToAccu == null || tblNeedToAccu.size() == 0) return tblQC;
		
		Integer[] iKeys = new Integer[tblNeedToAccu.size()];
		tblNeedToAccu.keySet().toArray(iKeys);
		
		for (int i = 0; i < iKeys.length; i++) {
			int iRuleId = iKeys[i];
			double[] dValues = tblNeedToAccu.get(iRuleId);
			
			QCRuleCal ruleCal = GetQCRuleUtil.getInstance(iRuleId);
			double[] dAccuRuleValues = ruleCal.calNBST(iRuleId, nbstVo, dValues);
			
			if (dAccuRuleValues != null) tblQC.put(iRuleId, dAccuRuleValues);
		}
		
		return tblQC;
	}
	
	// TODO 暫不作
	public void receveMth(String sProdId, int iOdrTp, long lShares, double dPrice) {}
	
	// General-Rollback
	public void receveRollbackGeneral(OdrInfo odrInfo, Hashtable<Integer, double[]> tblNeedToAccu) {
		/*
		 * TODO
		 * 進來要先加減共用的累計值, 這段暫時不作
		 * 2603 - 2606 才會用到，暫時不作 (國票不用)
		 */
		
		if (tblNeedToAccu == null || tblNeedToAccu.size() == 0) return ;
		
		Integer[] iKeys = new Integer[tblNeedToAccu.size()];
		tblNeedToAccu.keySet().toArray(iKeys);
		
		for (int i = 0; i < iKeys.length; i++) {
			int iRuleId = iKeys[i];
			double[] dValues = tblNeedToAccu.get(iRuleId);
			
			QCRuleCal ruleCal = GetQCRuleUtil.getInstance(iRuleId);
			ruleCal.calGeneralRollback(iRuleId, dValues, odrInfo);
		}
	}
	
}
